﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using YWTSSys.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using YWTSSys.Model;

namespace YWTSSys.DAL
{
    public class CustomerDAL
    {

        public static int Add(CustomerInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("Customer_Add", obj.客户名称, obj.会员卡号, obj.手机号码, obj.生日, obj.介绍人, obj.分组标志);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }

        public static DataTable GetByWhereTop20(string SqlWhere, string Sort)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("Customer_GetByWhereTop20", SqlWhere, Sort);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }

        public static IDataReader GetById(int 客户Id)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("Customer_GetById", 客户Id);
            return DB.ExecuteReader(dbCommand);
        }

     
        public static DataTable GetMyCustomerPagination(int UserId, string WhereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("Customer_GetMyCustomerPagination");
            DB.AddInParameter(dbCommand, "UserId", DbType.Int32, UserId);
            DB.AddInParameter(dbCommand, "WhereSql", DbType.String, WhereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String, Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32, 8);
            DataTable dt = DB.ExecuteDataSet(dbCommand).Tables[0];

            TotalCount = Convert.ToInt32(DB.GetParameterValue(dbCommand, "TotalCount"));
            return dt;
        }
        /// <summary>
        /// 重置指定分组的客户电话拔打状态为 未拔打
        /// </summary>
        /// <param name="GroupName"></param>
        /// <returns></returns>
        public static bool Customer_ChangeTelStatus(string GroupName)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("Customer_ChangeTelStatus", GroupName);
            try
            {
                DB.ExecuteNonQuery(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
